package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */

   @Autowired
   private ShoppingCartMapper shoppingCartMapper;

    public void insert( ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
//        添加userId赋值，来绑定唯一用户只能有一个购物车
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 先查询购物车表中是否有数据
      List<ShoppingCart > list= shoppingCartMapper.getById(shoppingCart);

        if(list!=null && list.size()>0){
            //有，购物车表商品数量加一，而且根据用户id与套餐id/菜品id查询商品有且只有一条数据
            ShoppingCart Cart = list.get(0);
            Integer number = Cart.getNumber();
                     number++;
            Cart.setNumber(number);
            //修改购物车表的商品数量
            shoppingCartMapper.updateNumber(Cart);

        }else{
            //没有，添加商品到购物车表中
            /**
             * 因为前端只有传进来套餐id、菜品id、口味
             * 但是并没有传入图片，商品的名称等，要自己查数据库对应的表
             * 然后赋值到购物车对象中，再把对象插入到购物车表中
             */
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
//            1.如果添加的为菜品,根据菜品id查询菜品信息
            if(dishId!=null){
               Dish dish= shoppingCartMapper.selectDish(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            }else{
//            2。如果添加的是套餐,根据套餐id查询套餐
                Setmeal setmeal=shoppingCartMapper.selectSetmeal(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }
    /**
     * 查看购物车
     * @return Carts
     */
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> Carts = shoppingCartMapper.getById(shoppingCart);
        return Carts;
    }

    /**
     * 清空目前用户的购物车
     */
    public void Clean() {
        Long UserId = BaseContext.getCurrentId();
        shoppingCartMapper.Clean(UserId);
    }

    /**
     * 删除购物车单个商品
     */
    public void delShop(ShoppingCartDTO shoppingCartDTO) {
//        先判断被删除的商品的数量是否大于1
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart > list= shoppingCartMapper.getById(shoppingCart);
//        获取到商品以及商品的数量
        ShoppingCart shop = list.get(0);
        Integer ShopNumber = shop.getNumber();
//        被删除的商品的数量大于1
        if(ShopNumber>1){
            ShopNumber--;
            shop.setNumber(ShopNumber);
            shoppingCartMapper.updateNumber(shop);
        }
//        被删除的商品数量=1
        else if (ShopNumber==1) {
            /**
             * 选择动态删除商品，因为删除的条件时可变的
             * 有可能根据菜品id与用户id删除也有可能是套餐id与用户id删除
             */
            shoppingCartMapper.delById(shop);
        }
    }
}
